package xyc.szk.servlet;
/*
 * @Description:
 * @Author: 山志凯
 * @Date: 2021-06-04
 * @Time: 15:03
 */

import xyc.szk.pojo.Users;
import xyc.szk.utils.JdbcUtils3;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet(value = "/user/detail")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取数据库表users中的id= id的数据记录，并封装到javabean user中
        //最终在在页面detail.jsp上显示这个记录所对应的对象
        String id = req.getParameter("id");
        try {
            Connection conn = JdbcUtils3.getConnection();
            String sql = "select * from users where id = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,Integer.parseInt(id));
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Users users = new Users();
                users.setId(rs.getInt("id"));
                users.setUserName(rs.getString("userName"));
                users.setUserPwd(rs.getString("UserPwd"));
                users.setAge(rs.getInt("age"));

                //请求转发将user这个javabean存入到request域 中、
                req.setAttribute("user", users);
                req.getRequestDispatcher("/detail.jsp").forward(req,resp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
